home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / Security - care / Protect / Tonto / tonto.c < prev    next >
C/C++ Source or Header  |  1991-11-26  |  9KB  |  410 lines

  1. /* 
  2.     Application management program 
  3.  
  4.     Copyright 1988 Greg Coleman, Purdue University
  5.     Extensive modifications by Dale Talcott
  6.         Purdue University Computing Center
  7.     These sources may be freely distributed provided this notice remains
  8.     intact.
  9. */
  10.  
  11. //# include    <Dialogs.h>
  12. //# include    <Events.h>
  13. //# include    <Menus.h>
  14. //# include    <Files.h>
  15. //# include    <StdFilePkg.h>
  16. # include    "Tonto.h"
  17.  
  18. /*
  19.  *    Define new launch param structure
  20.  */
  21. typedef struct {
  22.     StringPtr    pfName;
  23.     Integer        param;
  24.     char        LC[2];
  25.     Longint        extBlockLen;
  26.     Integer        fFlags;
  27.     Longint        launchFlags;
  28. } LaunchStruct;
  29.  
  30. /*
  31.  * Prototypes for local functions
  32.  */
  33. void CmdMode (void);
  34. void main (void);
  35. void myLaunch (LaunchStruct *);
  36. pascal void Res (void);
  37. void RunMode (void);
  38. void SkelInit (void);
  39.  
  40. /*
  41.  * Globals
  42.  */
  43. Integer        myRefNum;
  44. Integer        myVolRef;
  45. Handle        myParam;
  46. Str255        myName;
  47. Str255        myVolume;
  48.  
  49.  
  50. void
  51. main()
  52. {
  53.     OSErr            result;
  54.     HParamBlockRec    pb;
  55.     FCBPBRec        fc;
  56.     Str255            lx;
  57.  
  58.     SkelInit ();
  59.     /*
  60.      * Determine the name and refnum of the volume containing ourselves
  61.      */
  62.     GetAppParms (myName, &myRefNum, &myParam);
  63.     
  64.     ZERO (fc);
  65.     fc.ioNamePtr = lx;
  66.     lx[0] = 0;
  67.     fc.ioRefNum = myRefNum;
  68.     result = PBGetFCBInfo (&fc, FALSE);
  69.     if (result)
  70.         OSError ((SP)"\pCan't find ourselves", result, myName);
  71.  
  72.     ZERO (pb);
  73.     pb.volumeParam.ioVRefNum = fc.ioFCBVRefNum;
  74.     pb.volumeParam.ioNamePtr = myVolume;
  75.     myVolume[0] = 0;
  76.     result = PBHGetVInfo (&pb, FALSE);
  77.     if (result)
  78.         OSError ((SP)"\pCan't get vol info", result, myVolume);
  79.     myVolRef = pb.volumeParam.ioVRefNum;
  80.  
  81.     /*
  82.      * If the settings haven't been made permanent and the button
  83.      * is down, go into command mode.
  84.      * Otherwise, run the configured application.
  85.      */
  86.     if ( (GetNamedStr("\pperm", lx) == 0) && Button() )
  87.         CmdMode();
  88.     else
  89.         RunMode();
  90.     ExitToShell();
  91. }
  92.  
  93. void
  94. CmdMode()
  95. {
  96.     DialogPtr    dialog;
  97.     DialogPtr    aboutdialog, permdialog;
  98.     OSErr        result;
  99.     Integer        item;
  100.     char        *ts;
  101.     int            i;
  102.     char        tmp[5];
  103.     Integer        type;
  104.     Handle        theItem;
  105.     Str255        sline;
  106.     StringPtr    line = &sline[0];
  107.     Rect        box;
  108.     SFTypeList    msflist;
  109.     Point        mpt;
  110.     SFReply        myreply;
  111.     FInfo        myinfo;
  112.     Str255        path;
  113.     
  114.     Integer        vRefNum;
  115.  
  116.     Boolean        changed = false;
  117.  
  118.     HParamBlockRec    pb;
  119.     FCBPBRec        fc;
  120.     CInfoPBRec        ci;
  121.     CInfoPBRec        myci;
  122.  
  123. /*
  124.  *    Remember who and where we are
  125.  */            
  126.     ZERO (fc);
  127.     fc.ioNamePtr = line;
  128.     line[0] = 0;
  129.     fc.ioRefNum = myRefNum;
  130.     result = PBGetFCBInfo (&fc, FALSE);
  131.     ZERO (myci);
  132.     myci.hFileInfo.ioNamePtr = myName;
  133.     myci.hFileInfo.ioVRefNum = fc.ioFCBVRefNum;
  134.     myci.hFileInfo.ioDirID = fc.ioFCBParID;
  135.     result = PBGetCatInfo (&myci, FALSE);
  136. /*
  137.  *    Put up and fill in our main dialog
  138.  */
  139.     dialog = GetNewDialog(MAIN_DIALOG, (DialogPeek)0L, (WindowPtr)-1L);
  140.  
  141.     GetNamedStr("\papname", line);
  142.     SetText(dialog,APNAME,line);
  143.  
  144.     GetNamedStr("\pcreator", line);
  145.     SetText(dialog,CREATOR,line);
  146.     
  147.     GetNamedStr("\ppath", line);
  148.     SetText(dialog,PATH,line);
  149.     
  150.     ShowWindow (dialog);
  151. /*
  152.  *    Handle user actions
  153.  */
  154.     while(1)
  155.     {
  156.         ModalDialog(0L, &item);
  157.         switch(item)
  158.         {
  159.             case DONE:
  160.  
  161.                 if (changed == false)
  162.                     return;
  163.  
  164.                 /*
  165.                  * Get info about the application
  166.                  */
  167.                 ZERO (ci);
  168.                 ci.hFileInfo.ioNamePtr = myreply.fName;
  169.                 ci.hFileInfo.ioVRefNum = myreply.vRefNum;
  170.                 result = PBGetCatInfo (&ci, FALSE);
  171.                 if (result)
  172.                     OSError ((SP)"\pCan't get info about application ", result,
  173.                         (SP)myreply.vRefNum);
  174.  
  175.                 /*
  176.                  * Change our Finder creator info to match that
  177.                  * of the application we are subbing for.
  178.                  * (The test on the next line avoids changing it
  179.                  * if our name ends in pi.  This is to keep from
  180.                  * changing the creator of the project file when running
  181.                  * under the LightSpeed debugger.)
  182.                  */
  183.                 if (myName[myName[0]] != ('π' & 0xff)) {
  184.                     myci.hFileInfo.ioFlFndrInfo.fdCreator =
  185.                                 ci.hFileInfo.ioFlFndrInfo.fdCreator;
  186.                     myci.hFileInfo.ioFlFndrInfo.fdFlags &= ~fHasBundle;
  187.                     myci.hFileInfo.ioFDirIndex = 0;
  188.                     myci.hFileInfo.ioDirID = fc.ioFCBParID;
  189.                     result = PBSetCatInfo (&myci, FALSE);
  190.                     if (result)
  191.                         OSError ((SP)"\pCan't change our Finder info",
  192.                             result, (SP)"");
  193.                 }
  194.                 /*
  195.                  * Save away configuration strings from dialog settings
  196.                  */
  197.                 GetDItem (dialog, APNAME, &type, &theItem, &box);
  198.                 GetIText (theItem, line);
  199.                 SetNameStr ("\papname", line);
  200.                 
  201.                 GetDItem (dialog, CREATOR, &type, &theItem, &box);
  202.                 GetIText (theItem, line);
  203.                 SetNameStr ("\pcreator", line);
  204.                 
  205.                 GetDItem (dialog, PATH, &type, &theItem, &box);
  206.                 GetIText (theItem, line);
  207.                 SetNameStr ("\ppath", line);
  208.  
  209.                 return;
  210.                 
  211.             case ABOUT:
  212.                 aboutdialog = GetNewDialog(ABOUT_DIALOG,
  213.                                 (DialogPeek)0L, (WindowPtr)-1L);
  214.                 ShowWindow (aboutdialog);
  215.                 ModalDialog(0L, &item);
  216.                 DisposDialog(aboutdialog);
  217.                 break;
  218.             
  219.             case PERM:
  220.                 permdialog = GetNewDialog(PERM_DIALOG,
  221.                                 (DialogPeek)0L, (WindowPtr)-1L);
  222.                 ShowWindow (permdialog);
  223.                 ModalDialog(0L, &item);
  224.                 if (item == 1) 
  225.                 {
  226.                     DisposDialog(permdialog);
  227.                     continue;
  228.                 }
  229.                 SetNameStr("\pperm",(SP)"\pgreggc");
  230.                 return;
  231.                 
  232.             case BUILD:
  233.         
  234.             /*
  235.              *    Have the user locate the application to run
  236.              */
  237.                 msflist[0] = 'APPL';
  238.                 msflist[1] = '????';
  239.                 SetPt(&mpt, 100, 100);
  240.                 SFGetFile(mpt, (SP)NULL, (FileFilterProcPtr)NULL, 2,
  241.                             msflist, (ProcPtr)NULL, &myreply);
  242.                             
  243.                 if (myreply.good == false) 
  244.                     break;
  245.  
  246.                 /*
  247.                  * Put information about the application into the
  248.                  * dialog.
  249.                  * The trickiest part is setting the path to use to
  250.                  * locate the appl later.  If the appl is on the same
  251.                  * volume as we are, we delete the leading volume name
  252.                  * from the path.  If it is on a different volume, we
  253.                  * keep the whole path.
  254.                  * This allows identical, configured copies of Tonto
  255.                  * to be used on lab Macs, where all the local hard
  256.                  * disks are essentially the same, except that each
  257.                  * Mac's disk has a unique name.
  258.                  */
  259.                 ZERO(ci);
  260.                 ci.hFileInfo.ioNamePtr = myreply.fName;
  261.                 ci.hFileInfo.ioVRefNum = myreply.vRefNum;
  262.                 result = PBGetCatInfo (&ci, FALSE);
  263.                 if (result)
  264.                     OSError ((SP)"\pCan't get app info", result, myreply.fName);
  265.  
  266.                 ts = (char *)&ci.hFileInfo.ioFlFndrInfo.fdCreator;
  267.                 for (i = 0; i < 4; i++)
  268.                     tmp[i+1] = ts[i];
  269.                 tmp[0] = i;
  270.  
  271.                 pstrcpy (path, myreply.fName);
  272.                 pathname (path, myreply.vRefNum, sizeof (path)-1);
  273.                 if (path[0] != 0) {
  274.                     ZERO(pb);
  275.                     pb.volumeParam.ioVRefNum = myreply.vRefNum;
  276.                     pb.volumeParam.ioNamePtr = line;
  277.                     line[0] = 0;
  278.                     result = PBHGetVInfo(&pb, FALSE);
  279.                     if (result)
  280.                         OSError((SP)"\pCan't get vol info", result, line);
  281.                     if (pb.volumeParam.ioVRefNum == myVolRef) {
  282.                         i = line[0];
  283.                         pstrcpy (line, path);
  284.                         line[i] = path[0] - i;
  285.                         pstrcpy (path, line + i);
  286.                         if (path[0] == 0) {    /* if appl at top level */
  287.                             path[1] = ':';
  288.                             path[0] = 1;
  289.                         }
  290.                     }
  291.                     GetDItem(dialog, CREATOR, &type, &theItem, &box);
  292.                     SetIText(theItem,(SP)tmp);
  293.                     
  294.                     GetDItem(dialog, APNAME, &type, &theItem, &box);
  295.                     SetIText(theItem,myreply.fName);
  296.                 
  297.                     GetDItem(dialog, PATH, &type, &theItem, &box);
  298.                     SetIText(theItem, path);
  299.                     
  300.                     changed = true;
  301.                 }
  302.  
  303.                 break;        
  304.         }
  305.     }
  306.  
  307. }
  308.  
  309. void
  310. RunMode()
  311. {
  312.     OSErr    result;
  313.     Integer    item;                        /* dialog result */
  314.     Str255    line;                        /* temp */
  315.     Str255    path;                        /* partial path name */
  316.     DialogPtr    errdialog;
  317.     WDPBRec            wd;
  318.     CInfoPBRec        ci;                    /* file catalog info */
  319.     LaunchStruct    ls;                    /* launch params */
  320.  
  321.     /*
  322.      * Fetch the saved path.
  323.      * If it begins with ':', prepend the volume name we are from.
  324.      */
  325.     while (1) {                        /* dummy loop */
  326.         GetNamedStr ("\ppath", path);
  327.         if (path[0] == 0) {
  328.             result = 0;
  329.             break;
  330.         }
  331.         if (path[1] == ':') {
  332.             pstrcpy(line, path);
  333.             if (path[0] + myVolume[0] >= sizeof (path))
  334.                 break;
  335.             pstrcat2(path, myVolume, line);
  336.         }
  337.         ZERO (wd);
  338.         wd.ioNamePtr = path;
  339.         wd.ioWDProcID = 'ERIK';
  340.         result = PBOpenWD (&wd, FALSE);
  341.         if (result)
  342.             break;
  343.         result = SetVol ((SP)NULL, wd.ioVRefNum);
  344.         if (result)
  345.             break;
  346.     
  347.         GetNamedStr ("\papname", path);
  348.         if (path[0] == 0)
  349.             break;
  350.     
  351.         ZERO (ci);
  352.         ci.hFileInfo.ioNamePtr = path;
  353.         ci.hFileInfo.ioVRefNum = wd.ioVRefNum;
  354.         result = PBGetCatInfo (&ci, FALSE);
  355.         
  356.         if (result == noErr) {
  357.             ZERO (ls);
  358.             ls.pfName = path;
  359.             ls.LC[0] = 'L'; ls.LC[1] = 'C';
  360.             ls.extBlockLen = 6;
  361.             ls.fFlags = ci.hFileInfo.ioFlFndrInfo.fdFlags;
  362.             myLaunch (&ls);
  363.             /*NOTREACHED*/
  364.         }
  365.         break;
  366.     }
  367.     if (result) {
  368.         NumToString ((long)result, line);
  369.         item = FakeAlert ((SP)"\pCannot find application \"", path,
  370.             (SP)"\p\" error = ", line, 2, 1,
  371.             (SP)"\pQuit", (SP)"\pConfigure", (SP)"\p");
  372.         if (item == 2)
  373.             result = 0;
  374.     }
  375.     if (result == 0)
  376.         CmdMode();
  377. }
  378.  
  379. void
  380. myLaunch (ls)
  381. register LaunchStruct    *ls;
  382. {
  383.     asm {
  384.         movea.l    ls,a0
  385.         _Launch
  386.         _ExitToShell
  387.     }
  388.     /*NOTREACHED*/
  389. }
  390.  
  391.  
  392. pascal void
  393. Res () 
  394. {
  395.     ExitToShell();
  396.     /*NOTREACHED*/
  397. }
  398.  
  399. void
  400. SkelInit ()
  401. {
  402.     MaxApplZone ();
  403.     InitGraf ((Ptr) &thePort);
  404.     InitFonts ();
  405.     InitWindows ();
  406.     InitMenus ();
  407.     TEInit ();
  408.     InitDialogs (Res);        
  409.     InitCursor ();
  410. }